﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinStudent
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 检查窗体是否已经打开
        /// </summary>
        /// <param name="formName"></param>
        /// <returns></returns>
        private bool CheckForm(string formName)
        {
            bool b = false;
            foreach (Form item in Application.OpenForms)
            {
                if (item.Name == formName)
                {
                    b = true;
                    item.Activate();
                    break;
                }
            }
            return b;
        }

        /// <summary>
        /// 新增学生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddStudent_Click(object sender, EventArgs e)
        {
            //判断窗体是否已经打开          
            if (!CheckForm(typeof(FrmAddStudent).Name))
            {
                FrmAddStudent frmAddStudent = new FrmAddStudent();
                frmAddStudent.MdiParent = this;
                frmAddStudent.Show();//顶级窗体  不能显示到MDI容器中
            }          
        }

        /// <summary>
        /// 学生列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StudentList_Click(object sender, EventArgs e)
        {
            //单例
            //FrmStudentList frmStudentList = FrmStudentList.CreateInstance();
            //frmStudentList.MdiParent = this;
            //frmStudentList.Show();
            //frmStudentList.Activate();

            if (!CheckForm(typeof(FrmStudentList).Name))
            {
                FrmStudentList frmStudentList = new FrmStudentList();
                frmStudentList.MdiParent = this;
                frmStudentList.Show();
            }
        }

        /// <summary>
        /// 新增课程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddCourse_Click(object sender, EventArgs e)
        {
            if (!CheckForm(typeof(FrmAddCourse).Name))
            {
                FrmAddCourse frmAddClass = new FrmAddCourse();
                frmAddClass.MdiParent = this;
                frmAddClass.Show();
            }
        }

        /// <summary>
        /// 课程列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CourseList_Click(object sender, EventArgs e)
        {
            if (!CheckForm(typeof(FrmCourseList).Name))
            {
                FrmCourseList frmCourseList = new FrmCourseList();
                frmCourseList.MdiParent = this;
                frmCourseList.Show();
            }
        }

        /// <summary>
        /// 新增选课成绩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddSCList_Click(object sender, EventArgs e)
        {
            if (!CheckForm(typeof(FrmAddSC).Name))
            {
                FrmAddSC frmAddSC = new FrmAddSC();
                frmAddSC.MdiParent = this;
                frmAddSC.Show();
            }
        }

        /// <summary>
        /// 选课成绩列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SCList_Click(object sender, EventArgs e)
        {
            if (!CheckForm(typeof(FrmSCList).Name))
            {
                FrmSCList frmSCList = new FrmSCList();
                frmSCList.MdiParent = this;
                frmSCList.Show();
            }
        }

        /// <summary>
        /// 窗体关闭，退出程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result= MessageBox.Show("确定退出?", "退出提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result==DialogResult.Yes)
            {
                Application.ExitThread();//退出当前线程上的消息循环
            }
            else
            {
                e.Cancel = true;//手动取消
            }
        }

        /// <summary>
        /// 退出系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


    }
}
